package com.yan.yanbi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yan.yanbi.common.BaseResponse;
import com.yan.yanbi.common.ResultUtils;
import com.yan.yanbi.constant.CommonConstant;
import com.yan.yanbi.model.dto.chart.ChartQueryRequest;
import com.yan.yanbi.model.dto.chart.GenChartByAiRequest;
import com.yan.yanbi.model.entity.Chart;
import com.yan.yanbi.model.vo.BiResponse;
import com.yan.yanbi.service.ChartService;
import com.yan.yanbi.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 异步化接口
 *
 * @author 86177
 */
@RestController
@RequestMapping("/queue")
@Slf4j
@Profile({"dev", "local"})
//@Profile({"dev","local"})是Spring框架中用于配置文件加载的注解之一。
// 这个注解表示在指定的环境下（例如"dev"和"local"）才会生效，其他环境下不会被加载。
// 这样可以根据不同的环境加载不同的配置文件，方便开发人员对应用程序进行调试和测试。
public class QueueController {

    @Resource
    private ChartService queueService;

    @PostMapping("/gen")
    public BaseResponse<BiResponse> genChartByAi(@RequestPart("file") MultipartFile multipartFile,
                                                 GenChartByAiRequest genChartByAiRequest, HttpServletRequest request) {

        BiResponse biResponse = queueService.genChartAndSaveDb(multipartFile, genChartByAiRequest, request);
        return ResultUtils.success(biResponse);

    }


    /**
     * 获取查询包装类
     *
     * @param queueQueryRequest
     * @return
     */
    private QueryWrapper<Chart> getQueryWrapper(ChartQueryRequest queueQueryRequest) {
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        if (queueQueryRequest == null) {
            return queryWrapper;
        }
        Long id = queueQueryRequest.getId();
        String name = queueQueryRequest.getChartName();
        String goal = queueQueryRequest.getGoal();
        String queueType = queueQueryRequest.getChartType();
        Long userId = queueQueryRequest.getUserId();
        String sortField = queueQueryRequest.getSortField();
        String sortOrder = queueQueryRequest.getSortOrder();

        queryWrapper.eq(id != null && id > 0, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(name), "queueName", name);
        queryWrapper.eq(StringUtils.isNotBlank(goal), "goal", goal);
        queryWrapper.eq(StringUtils.isNotBlank(queueType), "queueType", queueType);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);
        queryWrapper.eq("isDelete", false);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

}
